﻿<template>
  <a-modal
    title="编辑团购单位管理"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['code']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['name']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['address']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['taxCode']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['telephone']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['contacts']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['bankName']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['bankAccount']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['remark']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['field01']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['field02']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['field03']" />
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择" v-decorator="['startDate']" @change="onChangestartDate"/>
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择" v-decorator="['endDate']" @change="onChangeendDate"/>
        </a-form-item>
        <a-form-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入" v-decorator="['rebateDefault']" />
        </a-form-item>
        <a-form-item v-show="false"><a-input v-decorator="['iD']" /></a-form-item>
        <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择创建时间" v-decorator="['cREATE_DATE']" @change="onChangecREATE_DATE"/>
        </a-form-item>
        <a-form-item label="创建者Id" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入创建者Id" v-decorator="['cREATE_USER_ID']" />
        </a-form-item>
        <a-form-item label="创建者名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入创建者名称" v-decorator="['cREATE_USER_NAME']" />
        </a-form-item>
        <a-form-item label="更新时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择更新时间" v-decorator="['mODIFY_DATE']" @change="onChangemODIFY_DATE"/>
        </a-form-item>
        <a-form-item label="修改者Id" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入修改者Id" v-decorator="['mODIFY_USER_ID']" />
        </a-form-item>
        <a-form-item label="修改者名称" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入修改者名称" v-decorator="['mODIFY_USER_NAME']" />
        </a-form-item>
        <a-form-item label="删除时间" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-date-picker style="width: 100%" placeholder="请选择删除时间" v-decorator="['dELETE_DATE']" @change="onChangedELETE_DATE"/>
        </a-form-item>
        <a-form-item label="删除者ID" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入删除者ID" v-decorator="['dELETE_USER_ID']" />
        </a-form-item>
        <a-form-item label="删除者姓名" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-input placeholder="请输入删除者姓名" v-decorator="['dELETE_USER_NAME']" />
        </a-form-item>
        <a-form-item label="软删除标记" :labelCol="labelCol" :wrapperCol="wrapperCol" has-feedback>
          <a-switch v-decorator="['iS_DELETED', { valuePropName: 'checked' }]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import moment from 'moment'
  import {
    IcCompanyInfoEdit
  } from '@/api/modular/main/IcCompanyInfoManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        startDateDateString: '',
        endDateDateString: '',
        cREATE_DATEDateString: '',
        mODIFY_DATEDateString: '',
        dELETE_DATEDateString: '',
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              iD: record.iD,
              code: record.code,
              name: record.name,
              address: record.address,
              taxCode: record.taxCode,
              telephone: record.telephone,
              contacts: record.contacts,
              bankName: record.bankName,
              bankAccount: record.bankAccount,
              remark: record.remark,
              field01: record.field01,
              field02: record.field02,
              field03: record.field03,
              rebateDefault: record.rebateDefault,
              cREATE_USER_ID: record.cREATE_USER_ID,
              cREATE_USER_NAME: record.cREATE_USER_NAME,
              mODIFY_USER_ID: record.mODIFY_USER_ID,
              mODIFY_USER_NAME: record.mODIFY_USER_NAME,
              dELETE_USER_ID: record.dELETE_USER_ID,
              dELETE_USER_NAME: record.dELETE_USER_NAME,
              iS_DELETED: record.iS_DELETED
            }
          )
        }, 100)
        this.form.getFieldDecorator('startDate', { initialValue: moment(record.startDate, 'YYYY-MM-DD') })
        this.startDateDateString = moment(record.startDate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('endDate', { initialValue: moment(record.endDate, 'YYYY-MM-DD') })
        this.endDateDateString = moment(record.endDate).format('YYYY-MM-DD')
        this.form.getFieldDecorator('cREATE_DATE', { initialValue: moment(record.cREATE_DATE, 'YYYY-MM-DD') })
        this.cREATE_DATEDateString = moment(record.cREATE_DATE).format('YYYY-MM-DD')
        this.form.getFieldDecorator('mODIFY_DATE', { initialValue: moment(record.mODIFY_DATE, 'YYYY-MM-DD') })
        this.mODIFY_DATEDateString = moment(record.mODIFY_DATE).format('YYYY-MM-DD')
        this.form.getFieldDecorator('dELETE_DATE', { initialValue: moment(record.dELETE_DATE, 'YYYY-MM-DD') })
        this.dELETE_DATEDateString = moment(record.dELETE_DATE).format('YYYY-MM-DD')
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.startDate = this.startDateDateString
            values.endDate = this.endDateDateString
            values.cREATE_DATE = this.cREATE_DATEDateString
            values.mODIFY_DATE = this.mODIFY_DATEDateString
            values.dELETE_DATE = this.dELETE_DATEDateString
            IcCompanyInfoEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败：' + JSON.stringify(res.message))
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      onChangestartDate(date, dateString) {
        this.startDateDateString = dateString
      },
      onChangeendDate(date, dateString) {
        this.endDateDateString = dateString
      },
      onChangecREATE_DATE(date, dateString) {
        this.cREATE_DATEDateString = dateString
      },
      onChangemODIFY_DATE(date, dateString) {
        this.mODIFY_DATEDateString = dateString
      },
      onChangedELETE_DATE(date, dateString) {
        this.dELETE_DATEDateString = dateString
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
